package com.java.eight.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.*;

/**
* @description: lambda表达式使用
* @author: xiaoliang.liu
* @date: 2018/7/8 13:47
* @version v1.0
*/
public class LambdaUsage {

    /**
     * 过滤(根据颜色)
     * @param apples 苹果集合
     * @param predicate 断言
     * @return list
     */
    private static List<Apple> filter(List<Apple> apples, Predicate<Apple> predicate) {
        ArrayList<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            if (apple != null) {
                if (predicate.test(apple)) {
                    list.add(apple);
                }
            }
        }
        return list;
    }

    /**
     * 过滤(根据重量)
     * @param apples 苹果集合
     * @param predicate 断言
     * @return list
     */
    private static List<Apple> filterByWeight(List<Apple> apples, LongPredicate predicate) {
        ArrayList<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            if (apple != null) {
                if (predicate.test(apple.getWeight())) {
                    list.add(apple);
                }
            }
        }
        return list;
    }

    /**
     * 过滤(根据颜色和重量)
     * @param apples 苹果集合
     * @param predicate 断言
     * @return list
     */
    private static List<Apple> filterByCondition(List<Apple> apples, BiPredicate<String, Long> predicate) {
        ArrayList<Apple> list = new ArrayList<>();
        for (Apple apple : apples) {
            if (apple != null) {
                if (predicate.test(apple.getColor(), apple.getWeight())) {
                    list.add(apple);
                }
            }
        }
        return list;
    }


    /**
     *  根据苹果集合做一些操作
     * @param apples 苹果集合
     * @param consumer 消费
     */
    private static void simpleTestConsumer(List<Apple> apples, Consumer<Apple> consumer) {
        for (Apple apple : apples) {
            if (apple != null) {
                consumer.accept(apple);
            }
        }
    }

    /**
     *  根据苹果做一些操作,返回操作后的苹果
     * @param apple 苹果
     * @param function 函数
     */
    private static String testFunction(Apple apple, Function<Apple, String> function) {
        return function.apply(apple);
    }


    /**
     *  get一个苹果
     * @param supplier 提供者
     */
    private static Apple testSupplier(Supplier<Apple> supplier) {
        return supplier.get();
    }
    
    
    /** 主方法 */
    public static void main(String[] args) {
        List<Apple> apples = Arrays.asList(new Apple("green", 25L), new Apple("red", 15L));

//        List<Apple> greenList = filter(apples, apple -> apple.getColor().equals("red"));
//        List<Apple> apples1 = filterByWeight(apples, weight -> weight > 16);
//        List<Apple> filterResult = filterByCondition(apples, (color, weight) -> color.equals("red") && weight > 16);
//
//        System.out.println(greenList);
//        System.out.println(apples1);
//        System.out.println(filterResult);

//        // 根据苹果集合做一些操作
//        simpleTestConsumer(apples, apple -> System.out.println(apple));
//         // 根据苹果做一些操作,返回操作后的苹果
//        String pinkApple = testFunction(new Apple("pink", 100L), apple -> apple.getWeight() * 100 + "");
//        System.out.println(pinkApple);


        // 使用suplier
        Supplier<String> s = String::new;
        System.out.println(s.get().getClass());

        Apple grayApple = testSupplier(() -> new Apple("gray", 24L));
        System.out.println(grayApple);

    }

}
